package convertor

import (
	"errors"
	"github.com/google/uuid"
	"github.com/jinzhu/copier"
	"gotham-plan-manage/internal/domain/model/plan-aggregate/aggregate"
	"gotham-plan-manage/internal/interfaces/response"
)

/*
   @author:zhongyang
   @date:2023/4/5
   @description:转换器-用于DO和DTO之间的相互转换
*/

type PlanManageConvertor struct {
}

// CreatePlanAggregate 创建计划聚合根
func (p *PlanManageConvertor) CreatePlanAggregate(name, startDate, endDate, parentId string) (*aggregate.PlanAggregate, error) {
	// 开始时间不能大于结束时间
	if startDate > endDate {
		return nil, errors.New("开始时间不能大于结束时间！")
	}
	return &aggregate.PlanAggregate{
		PlanId:    uuid.New().String(),
		Name:      name,
		StartDate: startDate,
		EndDate:   endDate,
		ParentId:  parentId,
	}, nil
}

// ConvertPlanResponse 将聚合根转换回DTO
func (p *PlanManageConvertor) ConvertPlanResponse(plan *aggregate.PlanAggregate) *response.PlanResponse {
	res := &response.PlanResponse{}
	_ = copier.Copy(&res, plan)
	return res
}

func (p *PlanManageConvertor) ConvertPlanDetailResponse(plan *aggregate.PlanAggregate) *response.PlanDetailResponse {
	return &response.PlanDetailResponse{
		PlanId: plan.PlanId,
		Target: plan.PlanDetail.Target,
	}
}

func (p *PlanManageConvertor) ConvertPlanReportResponse(plan *aggregate.PlanAggregate) *response.PlanReportResponse {
	return &response.PlanReportResponse{
		PlanName:   plan.Name,
		FinishDate: plan.FinishDate,
		IsOverTime: plan.PlanReport.IsOverTime,
		ExpectDay:  plan.PlanReport.ExpectDay,
		RealDay:    plan.PlanReport.RealDay,
		TaskTotal:  plan.PlanReport.TaskTotal,
	}
}

func (p *PlanManageConvertor) ConvertPlanPageResponse(plans []*aggregate.PlanAggregate) []*response.PlanPageResponse {
	var res []*response.PlanPageResponse
	for _, plan := range plans {
		item := &response.PlanPageResponse{
			PlanId:      plan.PlanId,
			Name:        plan.Name,
			CreateDate:  plan.CreateDate,
			StartDate:   plan.StartDate,
			EndDate:     plan.EndDate,
			FinishDate:  plan.FinishDate,
			PlanStatus:  plan.ExtendDetail.PlanStatus,
			TodoTotal:   plan.ExtendDetail.TaskTotal,
			SurplusTime: plan.ExtendDetail.SurplusTime,
			ParentId:    plan.ParentId,
		}
		for _, child := range plan.Children {
			childRes := &response.PlanPageResponse{
				PlanId:      child.PlanId,
				Name:        child.Name,
				CreateDate:  child.CreateDate,
				StartDate:   child.StartDate,
				EndDate:     child.EndDate,
				FinishDate:  child.FinishDate,
				PlanStatus:  child.ExtendDetail.PlanStatus,
				TodoTotal:   child.ExtendDetail.TaskTotal,
				SurplusTime: child.ExtendDetail.SurplusTime,
				ParentId:    child.ParentId,
			}
			item.Children = append(item.Children, childRes)
		}
		res = append(res, item)
	}
	return res
}

func (p *PlanManageConvertor) ConvertTaskItem(plan *aggregate.PlanAggregate) *response.TaskItemResponse {
	// 再做一次安全校验
	if len(plan.Tasks) == 0 || len(plan.Tasks[0].TaskItems) == 0 {
		return nil
	}
	return &response.TaskItemResponse{
		TaskItemId: plan.Tasks[0].TaskItems[0].Id,
		Content:    plan.Tasks[0].TaskItems[0].Content,
		TodoTaskId: plan.Tasks[0].TaskId,
		Level:      plan.Tasks[0].TaskItems[0].Level,
		PlanId:     plan.PlanId,
		FinishDate: plan.Tasks[0].TaskItems[0].FinishDate,
	}
}

func (p *PlanManageConvertor) ConvertTaskList(plan *aggregate.PlanAggregate) []*response.TaskListResponse {
	var res []*response.TaskListResponse
	for _, task := range plan.Tasks {
		taskRes := &response.TaskListResponse{
			TodoTaskId:  task.TaskId,
			PlanId:      plan.PlanId,
			Think:       task.Think,
			ExecuteDate: task.ExecuteDate,
		}
		for _, item := range task.TaskItems {
			itemRes := &response.TaskItemResponse{
				TaskItemId: item.Id,
				Content:    item.Content,
				TodoTaskId: taskRes.TodoTaskId,
				Level:      item.Level,
				PlanId:     plan.PlanId,
				FinishDate: item.FinishDate,
			}
			taskRes.TaskItems = append(taskRes.TaskItems, itemRes)
		}
		res = append(res, taskRes)
	}
	return res
}

func (p *PlanManageConvertor) ConvertQuestions(plan *aggregate.PlanAggregate) []*response.QuestionResponse {
	var res []*response.QuestionResponse
	copier.Copy(&res, plan.Questions)
	return res
}

func (p *PlanManageConvertor) ConvertTaskItemList(plan *aggregate.PlanAggregate) []*response.TaskItemResponse {
	var res []*response.TaskItemResponse
	for _, task := range plan.Tasks {
		for _, item := range task.TaskItems {
			resItem := &response.TaskItemResponse{
				TaskItemId: item.Id,
				Content:    item.Content,
				TodoTaskId: task.TaskId,
				Level:      item.Level,
				PlanId:     plan.PlanId,
				FinishDate: item.FinishDate,
			}
			res = append(res, resItem)
		}
	}
	return res
}
